﻿using System;
using SilentWebsite.Model.Contracts;
using SilentWebsite.Web;

namespace SilentWebsite.Model.Services
{
    public class DataServiceClient : IDataService
    {
        public event EventHandler<ActionEventArg> Completed;
        protected virtual void OnCompleted(ActionEventArg e)
        {
            EventHandler<ActionEventArg> handler = Completed;
            if (handler != null)
                handler(this, e);
        }

        public void AddProject(Project project)
        {
            ServicesUnitOfWork.Current.Entity.AddProject(project,
                x => OnCompleted(new ActionEventArg { Status = Status.Created }), true);
        }
        public void UpdateProject(Project project)
        {
            ServicesUnitOfWork.Current.Entity.UpdateProject(project,
                x => OnCompleted(new ActionEventArg { Status = Status.Updated }), true);
        }
        public void DeleteProject(Project project)
        {
            ServicesUnitOfWork.Current.Entity.DeleteProject(project,
                x => OnCompleted(new ActionEventArg { Status = Status.Deleted }), true);
        }
        public void GetProjects(long userId)
        {
            ServicesUnitOfWork.Current.Entity.Load(ServicesUnitOfWork.Current.Entity.GetProjectsQuery(userId),
                x => OnCompleted(new ActionEventArg { Status = Status.Completed, Result = x.Entities }), true);
        }
        public void GetControls()
        {
            ServicesUnitOfWork.Current.Entity.Load(ServicesUnitOfWork.Current.Entity.GetControlsQuery(),
                x => OnCompleted(new ActionEventArg { Status = Status.Completed, Result = x.Entities }), true);
        }
    }
}
